package com.ssh;

public class AddTwoNumbers1_2 {
	public static void main(String[] args) {
		ListNode l1 = new ListNode(8);
		ListNode l2 = new ListNode(9);
		ListNode l3 = new ListNode(9);
		ListNode l4 = new ListNode(2);
		ListNode l5 = new ListNode(6);
		ListNode l6 = new ListNode(4);
		l1.next = l2;
		l2.next = l3;
		/*
		l4.next = l5;
		l5.next = l6;*/
		
		ListNode l = addTwoNumbers(l1, l4);
		while (l.next != null) {
			System.out.println(l.val);
			l = l.next;
		}
		System.out.println(l.val);
	}
	public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode pre = new ListNode(0);
		ListNode head = pre;
		int t = 0;
		while (l1 != null || l2 != null || t != 0) {
			int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 :l2.val) + t;
			int q = sum % 10;
			t = sum / 10;
			ListNode x = new ListNode(q);
			pre.next = x;
			pre = x;
			l1 = (l1 == null) ? l1 : l1.next;
			l2 = (l2 == null) ? l2 : l2.next;
		}
		return head.next;
	}
}

